/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.apt.matrimony.methods;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.apt.matrimony.db.MyConnection;

/**
 *
 * @author yoko
 */
public class login {
 public static int checkLogin(String userName, String password){
        Connection cnn=null;
        PreparedStatement stm=null;
        ResultSet rs=null;
        int status=0;
        String role="";
     try {
         String sql="Select * from Accounts where User_Name = ? and User_Password= ?";
         cnn=MyConnection.openConnection();
         stm=cnn.prepareStatement(sql);
         stm.setString(1, userName);
         stm.setString(2, password);
         rs=stm.executeQuery();
         if(rs.next()){
         role=rs.getString("Account_Role");
         if(role.equalsIgnoreCase("admin")){
             status=1;
         }
         if(role.equalsIgnoreCase("member")){
             status=2;
         }
         }
      } catch (Exception e) {
         System.out.println(e.getMessage());
     } finally{
         MyConnection.closeConnection(cnn, stm, rs);
     }
     return status;
    }
      public static Boolean checkFirstTimeLogin(String username){
        Connection cnn=null;
        PreparedStatement stm=null;
        ResultSet rs=null;
     try {
         String sql="Select * from Accounts where User_Name = ? and Login_Count=0";
         cnn=MyConnection.openConnection();
         stm=cnn.prepareStatement(sql);
         stm.setString(1, username);
         rs=stm.executeQuery();
         if(rs.next()){
            return true;
         }
      } catch (Exception e) {
         System.out.println(e.getMessage());
     } finally{
         MyConnection.closeConnection(cnn, stm, rs);
     }
     return false;
    }
       public static int getLoginCount(String username){
        Connection cnn=null;
        PreparedStatement stm=null;
        ResultSet rs=null;
        int count=-1;
     try {
         String sql="Select Login_Count from Accounts where User_Name = ?";
         cnn=MyConnection.openConnection();
         stm=cnn.prepareStatement(sql);
         stm.setString(1, username);
         rs=stm.executeQuery();
         if(rs.next()){
            count=rs.getInt(1);
         }
      } catch (Exception e) {
         System.out.println(e.getMessage());
     } finally{
         MyConnection.closeConnection(cnn, stm, rs);
     }
     return count;
    }
       public static void updateLoginCount(String username){
        Connection cnn=null;
        PreparedStatement stm=null;
        int oldcount=-1;
        int newcount=-1;
     try {
         oldcount=getLoginCount(username);
         newcount=oldcount+1;
         String sql="Update Accounts set Login_Count="+newcount+" where User_Name=?";
         cnn=MyConnection.openConnection();
         stm=cnn.prepareStatement(sql);
         stm.setString(1, username);
         stm.executeUpdate();
     } catch (Exception e) {
         System.out.println(e.getMessage());
     } finally{
         MyConnection.closeConnection(cnn, stm, null);
     }
     return;
     }
      public static void main(String[] args){
          updateLoginCount("member2");
      }
}
